<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/9
 * Time: 10:07
 */
namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\HttpClient;
use Helpers\XhException;

class Order {

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    public static $fields = [
        'orderState' => ["UNPAY" => '未支付', "PAID" => '已支付', "UNRECEIVE" => '未收到', "REFUNDING" => '退款中', "DONE" => '已完成', "CLOSED" => '订单关闭'],
        'proofState' => ['NONE' => '无需审核' , 'AUTHORIZING' => '等待审核' , 'REJECT' => '审核拒绝' , 'DONE' => '审核完成'],
        'tabState'   => ["UNPAY" => '未支付','PAID' => '待发货' , 'UNRECEIVE' => '待签收' ,"REFUNDING" => '退款中', 'DONE'=>'已签收' ,'CLOSED' => '订单关闭' ]
    ];

    /**
     * 初始化配置
     *
     * @throws \Exception
     *
     * create by ck 20180507
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('resServerInterface');//接口资源中心
            self::$url  = Tool::getValue($config, 'ordernServiceUrl'); //订单服务
            if (!self::$url) {
                throw new \Exception('订单服务url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 需要提供凭证的订单列表
     *
     * @param int $page
     * @param int $size
     * @param array $extraParams
     * @return array
     * @throws XhException
     * @throws \Exception
     * create by xjq 20180509
     */
    public static function getOrders($page = 1 , $size = 50 , $extraParams = []){
        self::init();
        $params = [
            'page' => $page-1,
            'size' => $size,
            'bizType' => Main::getConfigByName('webInterfaceParam')['COURSE_BAIFEN_BIZ_ID'],
        ];
        $params = array_merge($params,$extraParams);
        $response = self::$httpClient->get('/api/v1/pri/orders/',$params);

        $body = $response->toArray();
        if (!$body || $body['totalElements'] === null || $body['content'] === null){
            throw new XhException('无返回数据', $response);
        }
        $total = $body['totalElements'];
        $data  = $body['content'];
        return ['total' => $total, 'rows' => $data];
    }

    /**
     * 平板订单列表
     *
     * @param int $page
     * @param int $size
     * @param array $extraParams
     * @return array
     * @throws XhException
     * @throws \Exception
     *
     * create by xjq 20180509
     */
    public static function getTabOrders($page = 1 , $size = 50 , $extraParams = []){
        self::init();
        $params = [
            'page' => $page-1,
            'size' => $size,
            'bizType' => Main::getConfigByName('webInterfaceParam')['PAD_BAIFEN_BIZ_ID'],
        ];
        $params = array_merge($params,$extraParams);
        $response = self::$httpClient->get('/api/v1/pri/orders/',$params);

        $body = $response->toArray();
        if (!$body || $body['totalElements'] === null || $body['content'] === null){
            throw new XhException('无返回数据', $response);
        }
        $total = $body['totalElements'];
        $data  = $body['content'];
        return ['total' => $total, 'rows' => $data];
    }

    /**
     * 接受支付凭证
     *
     * @param $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     *
     * create by xjq 20180509
     */
    public static function passOrder($params = []){
        self::init();
        $response = self::$httpClient->patchJson('/api/v1/pri/orders/payment/proofs/accepted',$params);
        if(!$response->isSuccess()){
            return $response->toArray();
        }
        return ['code' => 200 , 'msg' => '接收支付凭证'];
    }


    /**
     * 拒绝订单支付凭证
     *
     * @param $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     *
     * create by xjq 20180509
     */
    public static function rejectOrder($params = []){
        self::init();
        $response = self::$httpClient->patchJson('/api/v1/pri/orders/payment/proofs/rejected' , $params );
        if(!$response->isSuccess()) {
            return $response->toArray();
        }
        return ['code'=> 200 , 'msg' => '拒绝成功'];
    }

    /**
     * 订单发货
     * @param array $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     * create by xjq 20180510
     */
    public static function deliverOrder($params = []){
        self::init();
        $response = self::$httpClient->patchJson('/api/v1/pri/orders/delivery/delivered',$params);
        if (! $response->isSuccess()) {
            return $response->toArray();
        }
        return ['code'=>200 , 'msg' => '订单已发货'];
    }


    /**
     * 获取订单详情
     *
     * @param array $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     *
     * create by xjq 20180516
     */
    public static function  orderDetail($params = []){
        self::init();
        $response = self::$httpClient->get('/api/v1/pri/orders/details',$params);
        if (! $response->isSuccess()) {
            return $response->toArray();
        }
        return ['code'=>200 , 'data' => $response->toArray()];
    }

    /**
     * 取消订单
     *
     * @param array $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     *
     * create by xjq 20180517
     */
    public static function cancelOrderAction($params = []){
        self::init();
        $response = self::$httpClient->patchJson('/api/v1/pri/orders/canceled' , $params );
        if(!$response->isSuccess()) {
            return $response->toArray();
        }
        return ['code'=> 200 , 'msg' => '取消成功'];
    }

    /**
     * 同意退款
     * @param array $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     * create by xjq 20180606
     */
    public static function refundAgree($params = []){
        self::init();
        $response = self::$httpClient->patchJson('/api/v1/pri/orders/payment/refund/agree' , $params );
        if(!$response->isSuccess()) {
            return $response->toArray();
        }
        return ['code'=> 200 , 'msg' => '通过退款'];
    }

    /**
     * 拒绝退款
     * @param array $params
     * @return array|bool|mixed|\stdClass
     * @throws \Exception
     * create by xjq 20180606
     */
    public static function refundRefuse($params = []){
        self::init();
        $response = self::$httpClient->patchJson('/api/v1/pri/orders/payment/refund/refuse' , $params );
        if(!$response->isSuccess()) {
            return $response->toArray();
        }
        return ['code'=> 200 , 'msg' => '拒绝退款'];
    }

}